<# 
    .SYNOPSIS
        Completely removes and reinstalls an entire site topology.

    .DESCRIPTION
        Part of the SharePoint Total Automation Toolkit (STAT). 
 
    .EXAMPLE
        FullSiteTopologyRedeploy -Company Contoso -Project Intranet -Release 1.0.0.0 -Environment Development

        If using the default configuration file path and structure, you can provide discrete parameters to each subdirectory.

    .EXAMPLE
        FullSiteTopologyRedeploy -ConfigPath 'C:\ProgramData\SharePoint Total Automation Toolkit\Configuration Files\Contoso\Intranet\1.0.0.0\Development'

        Using the -ConfiPath parameter allows you to directly specify the full path to the configuration files that control the process.

    .EXAMPLE
        $myFullPath | FullSiteTopologyRedeploy 

        This function accepts pipeline input for the -ConfiPath parameter which allows you to directly specify the full path to the configuration files that control the process.

    .PARAMETER Company
        The company subdirectory (e.g. Contoso)
        Part of the overall path to the configuration files that control the installation process

    .PARAMETER Project
        The project subdirectory (e.g. Intranet)
        Part of the overall path to the configuration files that control the installation process

    .PARAMETER Release
        The release subdirectory (e.g. 1.0.0.0)
        Part of the overall path to the configuration files that control the installation process

    .PARAMETER Environment
        The target environment subdirectory (e.g. Development)
        Part of the overall path to the configuration files that control the installation process

    .PARAMETER ConfigPath
        The full path to the configuration files that control the installation process
        (e.g. 'C:\ProgramData\SharePoint Total Automation Toolkit\Configuration Files\Contoso\Intranet\1.0.0.0\Development')

#>
[CmdletBinding(DefaultParametersetName="DiscreteValues")]
param([parameter(ParameterSetName = "DiscreteValues", 
                    Mandatory = $true,
                    HelpMessage = "The company subdirectory (e.g. Contoso). Part of the overall path to the configuration files that control the installation process")]
                    [string]$Company,
	    [parameter(ParameterSetName = "DiscreteValues", 
                    Mandatory = $true,
                    HelpMessage = "The project subdirectory (e.g. Intranet). Part of the overall path to the configuration files that control the installation process")]
                    [string]$Project,
	    [parameter(ParameterSetName = "DiscreteValues", 
                    Mandatory = $true,
                    HelpMessage = "The release subdirectory (e.g. 1.0.0.0). Part of the overall path to the configuration files that control the installation process")]
                    [string]$Release,
	    [parameter(ParameterSetName = "DiscreteValues", 
                    Mandatory = $true,
                    HelpMessage = "The target environment subdirectory (e.g. Development). Part of the overall path to the configuration files that control the installation process")]
                    [string]$Environment,
	    [parameter(ParameterSetName = "FullPath", 
                    Mandatory = $true,
                    ValueFromPipeline=$true,
                    ValueFromPipelineByPropertyName=$true, 
                    HelpMessage = "The full path to the configuration files that control the installation process. (e.g. 'C:\ProgramData\SharePoint Total Automation Toolkit\Configuration Files\Contoso\Intranet\1.0.0.0\Development'")]
                    [string]$ConfigPath,
	    [parameter(ParameterSetName = "DiscreteValues", 
                    Mandatory = $false,
                    HelpMessage = "Reprovisions the managed meta data service application")]
	    [parameter(ParameterSetName = "FullPath", 
                    Mandatory = $false,
                    HelpMessage = "Reprovisions the managed meta data service application")]
                    [switch]$ReprovisionManagedMetaDataApp,
	    [parameter(ParameterSetName = "DiscreteValues", 
                    Mandatory = $false,
                    HelpMessage = "Removes only the site collections")]
	    [parameter(ParameterSetName = "FullPath", 
                    Mandatory = $false,
                    HelpMessage = "Removes only the site collections")]
                    [switch]$PreserveWebApps)

switch ($PSCmdlet.ParameterSetName)
{
    "DiscreteValues"
    {
        $ConfigPath = $env:ProgramData + "\SharePoint Total Automation Toolkit\Configuration Files\" + $Company + "\" + $Project + "\" + $Release + "\" + $Environment
    }    
    "FullPath"
    {
    }
}

$STATHome = "${env:ProgramFiles(x86)}\SharePoint Total Automation Toolkit"
$stepByStepScriptPathRoot = "$STATHome\Step-By-Step\SharePoint"

if ($VerbosePreference -eq $true)
{
    $verboseFlag = " -Verbose"
}
else
{
    $verboseFlag = ""
}

# Prep the logging transcript file
$timeStamp = Get-Date -Format "yyyyMMdd-hhmm"
$logFileName = "FullSiteTopologyRedeploy-" + $timeStamp + ".log"
$logFilePath = $HOME + "\Documents\" + $logFileName

if ($PreserveWebApps -eq $true)
{
	#
	# Only delete the existing Site Collections
	#

    # Run once to remove site collections from the default list
	$path = $stepByStepScriptPathRoot + "\05 Utility Scripts and Tools"
	$script = "DeleteSiteCollections.ps1"
    powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


    # Run again to remove site collections from the additional list
	$path = $stepByStepScriptPathRoot + "\05 Utility Scripts and Tools"
	$script = "DeleteSiteCollections.ps1"
    powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath -UseAdditionalListInstead $verboseFlag
}
else
{
	#
	# Delete the existing web applications
	#

    # Run once to remove site collections from the default list
	$path = $stepByStepScriptPathRoot + "\05 Utility Scripts and Tools"
	$script = "DeleteWebApps.ps1"
    powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag

    # Run again to remove site collections from the additional list
	$path = $stepByStepScriptPathRoot + "\05 Utility Scripts and Tools"
	$script = "DeleteWebApps.ps1"
    powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath -UseAdditionalListInstead $verboseFlag
}

if ($ReprovisionManagedMetaDataApp -eq $true)
{
	#
	# Delete and recreate the existing Managed Metadata Service Application
	#
	$path = $stepByStepScriptPathRoot + "\05 Utility Scripts and Tools"
	$script = "CreateManagedMetadataServiceApps.ps1"
    powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath -DeleteIfExists $verboseFlag
}

#
# Perform and IIS Reset before attempting to remove the solutions
#
IISReset


#
# Remove the SharePoint Solutions
#
Write-Host "Restarting Timer Service..."
restart-service SPTimerV4

$path = $stepByStepScriptPathRoot + "\05 Utility Scripts and Tools"
$script = "RemoveWsps.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag

#
# Deploy SharePoint Solution Packages Phase 1
#
Write-Host "Restarting Timer Service..."
restart-service SPTimerV4

$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "15-DeployFirstPhaseWsps.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Setup the Trusted Providers
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "16-SetupTrustedProviders.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Create the Site Topology
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "17-CreateInitialSiteTopology.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Import the Managed Metadata
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "18-ImportManagedMetadata.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Setup the Content Type Hub
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "19-SetupContentTypeHub.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Deploy SharePoint Solution Packages Phase 2
#
Write-Host "Restarting Timer Service..."
restart-service SPTimerV4

$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "20-DeploySecondPhaseWsps.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Configure Variations
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "21-ConfigureVariations.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag
	
#
# Create the Remaining Site Topology
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "22-CreateAdditionalSiteTopology.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Deploy SharePoint Solution Packages Phase 3
#
Write-Host "Restarting Timer Service..."
restart-service SPTimerV4

$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "23-DeployThirdPhaseWsps.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Create New Lists and Libraries
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "24-CreateListsAndLibraries.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Configure Permissions and Groups
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "25-ConfigureSitePermissions.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Upload Site Content
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "26-ImportSiteContent.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Create SharePoint Publishing Pages
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "27-CreatePublishingPages.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Deploy SharePoint Solution Packages Phase 4
#
Write-Host "Restarting Timer Service..."
restart-service SPTimerV4

$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "28-DeployFourthPhaseWsps.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Create Search Crawled Properties and Managed Properties
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "29-CreateManagedPropertiesForSearch.ps1"
powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag


#
# Create Content Deployment Paths and Jobs
#
$path = $stepByStepScriptPathRoot + "\04 Setup Sites"
$script = "30-ConfigureContentDeployment.ps1"
#powershell -file (Join-Path $path $script) -ConfigPath $ConfigPath $verboseFlag
